package com.longdaji.decoration.ui.goodscategory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.data.bean.GoodsCategory;
import com.longdaji.decoration.ui.goodscategory.content.GoodsContentFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class GoodsCategoryActivity extends BaseActivity implements GoodsCategoryContract.View {

    private static final String TAG = GoodsCategoryActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.recycler_view_category)
    RecyclerView mRecyclerViewCategory;
    private List<GoodsCategory.SecondCategory> mCategoryList;
    private Fragment currentFragment;
    private List<Fragment> mFragments;

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, GoodsCategoryActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "GoodsCategoryActivity onCreate");
        setContentView(R.layout.activity_goods_catergory);
        setUnBinder(ButterKnife.bind(this));
        if (getIntent() != null) {
            //Log.d(TAG, "categoryList is " + getIntent().getParcelableArrayListExtra("category_list"));
            mCategoryList = getIntent().getParcelableArrayListExtra("category_list");

        }
        initView();

    }

    private void initView() {
        mTvTitle.setText("分类");
//        mCategoryList = new ArrayList<>();
//        mCategoryList.add("耳饰");
//        mCategoryList.add("眼镜");
//        mCategoryList.add("项链");
//        mCategoryList.add("戒指");
//        mCategoryList.add("手链");
//        mCategoryList.add("发卡");
        mFragments = new ArrayList<>();
        if (mCategoryList != null) {
            for (int i = 0; i < mCategoryList.size(); i++) {
                GoodsContentFragment fragment = new GoodsContentFragment();
                fragment.setCateId(mCategoryList.get(i).getCateId());
                mFragments.add(fragment);
            }
        }
        initReclerView();
        if (mFragments.size() != 0) {
            switchFragment(mFragments.get(0));
        }
    }

    private Fragment getCurFragment(int position) {
        if (mFragments != null && position >= 0 && position < mFragments.size()) {
            return mFragments.get(position);
        } else {
            return null;
        }

    }

    private void initReclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerViewCategory.setLayoutManager(layoutManager);
        GoodsCategoryAdapter adapter = new GoodsCategoryAdapter(mCategoryList);
        adapter.setOnItemClickListener(new GoodsCategoryAdapter.ItemClickListener() {
            @Override
            public void onClick(int position) {
                switchFragment(getCurFragment(position));
            }
        });
        mRecyclerViewCategory.setAdapter(adapter);

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void switchFragment(Fragment targetFragment) {

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (targetFragment != null) {
            if (!targetFragment.isAdded()) {
                //第一次使用switchFragment()时currentFragment为null，所以要判断一下
                if (currentFragment != null) {
                    transaction.hide(currentFragment);
                }
                transaction.add(R.id.flyt_goods_category, targetFragment).commit();

            } else {
                transaction.hide(currentFragment).show(targetFragment).commit();

            }
        }
        currentFragment = targetFragment;
    }


}
